<?php

/**
 * banner actions.
 *
 * @package    broadway
 * @subpackage banner
 * @author     singleton
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class bannerActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->isActive = $isActive = $request->getParameter('isActive', 'all');
    $this->position = $position = $request->getParameter('position');
    $this->pager = Doctrine::getTable('Banner')->getPager($position, $request->getParameter('page', 1), $isActive);
  }
  
  
  public function executeActivate(sfWebRequest $request)
  {
    $banner = Doctrine::getTable('Banner')->find($request->getParameter('id'));
    $this->forward404Unless($banner);

    $banner->setIsActive($request->getParameter('status'));
    $banner->save();

    $this->getUser()->setFlash('success', 'Амжилттай хадгалагдлаа.', true);
    $this->redirect('banner/index');
  }
  

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new BannerForm();
    $this->setTemplate('edit');
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new BannerForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($banner = Doctrine::getTable('Banner')->find(array($request->getParameter('id'))), sprintf('Object banner does not exist (%s).', $request->getParameter('id')));
    $this->form = new BannerForm($banner);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($banner = Doctrine::getTable('Banner')->find(array($request->getParameter('id'))), sprintf('Object banner does not exist (%s).', $request->getParameter('id')));
    $this->form = new BannerForm($banner);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $this->forward404Unless($banner = Doctrine::getTable('Banner')->find(array($request->getParameter('id'))), sprintf('Object banner does not exist (%s).', $request->getParameter('id')));
      
    try {
      // unlink files
      @unlink(sfConfig::get('sf_upload_dir').'/banner/'.$banner->getPath());
      
      $banner->delete();
      
      $this->getUser()->setFlash('success', 'Амжилттай устлаа.', true);
    }catch (Exception $e){}
    
    $this->redirect('banner/index');
    
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $banner = $form->save();

      $ext = myTools::getFileExtension($banner->getPath());
      $banner->setExt($ext);
      $banner->setIsActive(1);
      $banner->save();
      
      //$this->resizeImages($ext, $form->getValue('position'), $banner->getPath()); // should resize ?
      
      $this->getUser()->setFlash('success', 'Амжилттай хадгалагдлаа.', true);
      
      $this->redirect('banner/index');
    }
  }
  
  protected function resizeImages($ext, $position, $fileName)
  {
    if(in_array($ext, array('jpg', 'jpeg', 'png', 'gif', 'JPG', 'JPEG', 'PNG', 'GIF')))
    {
      $width = 300; //default width
      switch ($position)
      {
        case "home": if($width > 276) $width = 276; break;
        case "inner": if($width > 435) $width = 435; break;
      }
      
      $upload_dir = sfConfig::get('sf_upload_dir').'/zzz/';    
    
      if ($fileName && file_exists($upload_dir.$fileName))
      {
        $thumbnail = new sfThumbnail($width, null, true, false, 100);
        $thumbnail->loadFile($upload_dir.$fileName);
        $thumbnail->save($upload_dir.$fileName);
      }

    } // not an image file
  }
  
}